package com.sgb.kjwl.view.ui.activity.self.packers;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivitySelfTeamIntroduceEditBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.SelfTeamIntroduceEntity;
import com.sgb.kjwl.view.ui.adapter.self.packers.FlowLayoutAdapter1;
import com.sgb.kjwl.view.ui.adapter.self.packers.FlowLayoutAdapter2;
import com.sgb.kjwl.view.widget.MyItemDecoration;
import com.sgb.kjwl.viewmodel.self.packers.SelfTeamIntroduceEditModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.FlowLayoutManager;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;

import javax.inject.Inject;

/**
 * description:工队简介
 * @author WYJ
 * @date 2020-11-18
 */
public class SelfTeamIntroduceEditActivity extends AppActivity {

    @Inject
    public SelfTeamIntroduceEditModel mModel;
    private ActivitySelfTeamIntroduceEditBinding mBind;
    private SelfTeamIntroduceEntity mEntity;

    private FlowLayoutAdapter1 mAdapter1;
    private FlowLayoutAdapter2 mAdapter2;

    public static void start(Context context) {
        Intent intent = new Intent(context, SelfTeamIntroduceEditActivity.class);
        context.startActivity(intent);
    }

    public static void start(Context context, SelfTeamIntroduceEntity entity) {
        Intent intent = new Intent(context, SelfTeamIntroduceEditActivity.class);
        intent.putExtra(AppConstant.EXTRA_ENTITY, entity);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        mEntity = (SelfTeamIntroduceEntity) getIntent().getSerializableExtra(AppConstant.EXTRA_ENTITY);

        int companyType = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        if (companyType == 2){
            mEntity.setTeamNo(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, ""));
        }

        mBind = DataBindingUtil.setContentView(this, R.layout.activity_self_team_introduce_edit);
        mBind.setViewModel(mEntity);
        mBind.btnRelease.setOnClickListener(this);
        mBind.teamPublish.setOnClickListener(this);

        mAdapter1 = new FlowLayoutAdapter1(this, new ArrayList<>());
        mBind.rvBusiness.setLayoutManager(new FlowLayoutManager());
        mBind.rvBusiness.addItemDecoration(new MyItemDecoration());
        mBind.rvBusiness.setAdapter(mAdapter1);

        mAdapter2 = new FlowLayoutAdapter2(this, new ArrayList<>());
        mBind.rvArea.setLayoutManager(new FlowLayoutManager());
        mBind.rvArea.addItemDecoration(new MyItemDecoration());
        mBind.rvArea.setAdapter(mAdapter2);
        if (mEntity != null) {
            updateIntroduceEntity(mEntity);
        } else {
            mModel.getTeamIntroduce();
        }
    }

    @Override
    public void onMultiClick(View v) {
        super.onMultiClick(v);
        int id = v.getId();
        if (id == R.id.btn_release) {
            SelfTeamIntroduceActivity.start(this, mEntity, AppConstant.PAGER_EDIT);
            finish();
        } else if (id == R.id.team_publish) {
            SelfTeamIntroduceActivity.start(this, null, AppConstant.PAGER_ADD);
            finish();
        }
    }

    public void updateIntroduceEntity(SelfTeamIntroduceEntity data) {
        mEntity = data;
        if (mEntity == null) {
            //【无工队】工队简介内容为发布状态，可以进行发布
            //【有工队】工队简介内容为展示状态，可以进行编辑
            mBind.layoutNullTeam.setVisibility(View.VISIBLE);
        } else {
            mBind.setViewModel(mEntity);
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.packer_avatar)
                    .transform(new CircleCrop());
            if (TextUtils.isEmpty(mEntity.getAvatarAddress())) {
                Glide.with(this).load(R.mipmap.packer_avatar).into(mBind.ivAvatar);
            } else {
                Glide.with(this).load(mEntity.getAvatarAddress()).apply(options).into(mBind.ivAvatar);
            }
            mAdapter1.setDatas(mEntity.getBusinessScopes());
            mAdapter2.setDatas(mEntity.getServiceAreas());
        }
    }

    public void showEmpty() {
        mBind.emptyView.setVisibility(View.VISIBLE);
    }
}
